package sort;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import sort.mergesort.MergeSort;
import sort.quiksort.QuikSort;
import sort.util.RamdomUtil;
import sort.util.StopWatch;

/**
 * 测试工具（图）
 */
public class TimeSeriesChart extends ApplicationFrame {


    public TimeSeriesChart(String applicationTitle ){
        super(applicationTitle);
        DefaultCategoryDataset xydataset = createDataset();
        JFreeChart lineChart = ChartFactory.createLineChart("归并排序与快排比较", "数据量","用时（秒）",xydataset, PlotOrientation.VERTICAL,
                true,true,false);
        ChartPanel chartPanel = new ChartPanel( lineChart );
        chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
        setContentPane( chartPanel );


    }
    private static DefaultCategoryDataset createDataset() {

        StopWatch first;

        DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
        int[] mergedata=null;
        int[] quikdata=null;
        for (int inc = 1000000; inc <= 80000000; inc += 1000000) {
            mergedata = RamdomUtil.generateRandomArray(inc);
            quikdata = RamdomUtil.generateRandomArray(inc);
            long startTimeMillis = System.currentTimeMillis();
            MergeSort.sort(mergedata);
            long lastTime = System.currentTimeMillis() - startTimeMillis;
            dataset.addValue( lastTime , "MergeSort2" , String.valueOf(inc/100000) );

            long startTimeMillis2 = System.currentTimeMillis();
            QuikSort.sort(quikdata);
            long lastTime2 = System.currentTimeMillis() - startTimeMillis2;
            dataset.addValue( lastTime2 , "QuickSort" , String.valueOf(inc/100000) );
            System.out.println("mergesort:   "+lastTime+"   quiksort:   "+lastTime2);
        }

        return dataset;
    }
    public static void main(String[] args) {
        TimeSeriesChart chart = new TimeSeriesChart(
                "School Vs Years");

        chart.pack( );
        RefineryUtilities.centerFrameOnScreen( chart );
        chart.setVisible( true );

    }
}
